package com.yoyo.itravel.service.impl;

import com.yoyo.itravel.bean.CommentImg;
import com.yoyo.itravel.bean.SightComment;
import com.yoyo.itravel.dao.ICommentImgDao;
import com.yoyo.itravel.dao.ISightCommentDao;
import com.yoyo.itravel.dao.IUserDao;
import com.yoyo.itravel.exceptions.SightCommentException;
import com.yoyo.itravel.service.ISightCommentService;
import com.yoyo.itravel.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;

/**
 * @Description 景点评论服务层接口实现类
 * @Author HZX
 * @Version 1.0
 * @Date 2020/6/18 18:21
 */
@Service
public class SightCommentServiceImpl implements ISightCommentService {

    @Autowired
    private ISightCommentDao sightCommentDao;
    @Autowired
    private ICommentImgDao commentImgDao;
    @Autowired
    private IUserDao userDao;

    /**
     * @Description 插入评论数据
     *
     * @Param sightComment
     * @Param imgPaths
     * @Return boolean
     */
    @Override
    public boolean addComment(SightComment sightComment, String[] imgPaths) throws SightCommentException {
        if (sightComment == null) {
            throw new SightCommentException("景点评论对象为null...");
        }
        try {
            // 生成评论id
            String commentId = StringUtils.getRandomUUID();
            sightComment.setSightCommentId(commentId);
            // 添加景点评论
            sightComment.setLikeNum(0);
            sightCommentDao.insert(sightComment);
            // 遍历图片路径数组
            for (String imgPath : imgPaths) {
                if (StringUtils.isEmpty(imgPath)) {
                    // 如果图片路径为空，则跳过此次循环，不进行插入操作
                    continue;
                }
                // 构建评论图片对象
                CommentImg commentImg = new CommentImg(StringUtils.getRandomUUID(), imgPath, commentId);
                // 添加评论图片
                commentImgDao.insert(commentImg);
            }

            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new SightCommentException("插入评论数据失败！");
        }
    }

    /**
     * @Description 通过评论id删除景点评论数据
     *
     * @Param commentId
     * @Return boolean
     */
    @Override
    public boolean deleteByCommentId(String commentId) throws SightCommentException {
        return false;
    }

    /**
     * @Description 通过评论id查询景点评论数据
     *
     * @Param commentId
     * @Return com.yoyo.itravel.bean.SightComment
     */
    @Override
    public SightComment findByCommentId(String commentId) throws SightCommentException {
        if (StringUtils.isEmpty(commentId)) {
            throw new SightCommentException("景点评论id为空...");
        }
        try {
            return sightCommentDao.selectByCommentId(commentId);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new SightCommentException("通过评论id查询景点评论数据失败！");
        }
    }

    /**
     * @Description 通过用户id查询该用户发表的所有的对景点的评论
     *
     * @Param userId
     * @Return java.util.List<com.yoyo.itravel.bean.SightComment>
     */
    @Override
    public List<SightComment> findAllByUserId(String userId) throws SightCommentException {
        if (StringUtils.isEmpty(userId)) {
            throw new SightCommentException("用户id为空...");
        }
        try {
            return sightCommentDao.selectAllByUserId(userId);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new SightCommentException("通过用户id查询该用户发表的所有的对景点的评论失败！");
        }
    }

    /**
     * @Description 通过景点id获得该景点的所有评论
     *
     * @Param sightId
     * @Param star
     * @Return java.util.List<com.yoyo.itravel.bean.SightComment>
     */
    @Override
    public List<SightComment> findAllBySightIdAndStar(String sightId, Integer star) throws SightCommentException {
        if (StringUtils.isEmpty(sightId)) {
            throw new SightCommentException("景点id为空...");
        }

        try {
            // 通过景点id获得该景点的所有评论
            List<SightComment> comments = sightCommentDao.selectAllBySightIdAndStar(sightId, star);

            if (comments == null) {
                return null;
            }

            // 遍历评论集合
            for (SightComment comment : comments) {
                // 设置评论id查找该评论的发布者
                comment.setUser( userDao.selectByUserId(comment.getUserId()));
                // 通过评论id获得该评论的所有图片
                List<CommentImg> commentImgs = commentImgDao.selectAllByCommentId(comment.getSightCommentId());
                // 将图片添加到对应的评论中
                comment.setCommentImgs(commentImgs);
            }

            return comments;

        } catch (SQLException e) {
            e.printStackTrace();
            throw new SightCommentException("通过景点id获得该景点的所有评论失败！");
        }
    }

    /**
     * @Description 通过评论id修改点赞数
     *
     * @Param commentId
     * @Param isAdd
     * @Return boolean
     */
    @Override
    public boolean updateLikeNum(String commentId, boolean isAdd) throws SightCommentException {
        if (StringUtils.isEmpty(commentId)) {
            throw new SightCommentException("评论id为空...");
        }
        try {
            return sightCommentDao.updateLikeNum(commentId, isAdd) == 1;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new SightCommentException("通过评论id修改点赞数失败！");
        }
    }
}
